package cn.maka.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.util.Random;
import java.awt.Color;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;


@WebServlet("/checkServlet")
public class CheckServlet extends HttpServlet {
    public static final String typeB="0123456789ABCDEFGHRJKLMNOPQRSTUVWXYZ";
    public static final int Font_SIZE=15;
    public static final String FONT_NAME="Fixedsys";

    //设置图片宽和高
    private int width=100;
    private int height=30;

    //设置随机数类
    Random random=new Random();

    //设置干扰线数量
    private int lineNum=10;

    //设置字符个数
    private int charNum=4;

    //验证码字符串
    StringBuilder stringBuilder = new StringBuilder();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedImage tmpImage=this.getBufferedImage();
        ImageIO.write(tmpImage,"JPEG",response.getOutputStream());
        HttpSession session = request.getSession();

        //System.out.println(stringBuilder.toString());
        session.setAttribute("checkCode",stringBuilder.toString());

        stringBuilder = new StringBuilder();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    public BufferedImage getBufferedImage(){
        BufferedImage imageBuffer= new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_BGR);



        //定义一个Graphise类对封闭图像上绘制
        Graphics g= imageBuffer.getGraphics() ;
        //设定背景颜色
        g.setColor(getColor(200,250));

        //设定填充区域的矩形框
        g.fillRect(0,0,this.width,this.height);

        //设置干扰色的颜色
        g.setColor(getColor(100,120));
        //绘制干扰线
        for(int i=0;i<lineNum;i++){
            drawLine(g);
        }


        g.setFont(new Font(FONT_NAME, Font.ROMAN_BASELINE, Font_SIZE));
        //绘制字符串
        for(int i=0;i<charNum;i++){
            g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
            String imageString=String.valueOf(typeB.charAt(random.nextInt(typeB.length()))) ;
            g.translate(random.nextInt(3), random.nextInt(3));//平移原点，使每次生成的字符位置错位
            g.drawString(imageString, 20*i, 16) ;

            //System.out.println(typeB.charAt(random.nextInt(typeB.length())));

            stringBuilder.append(imageString);

            //System.out.println("???:"+stringBuilder);
        }
        g.dispose() ;//释放资源

        return imageBuffer;

    }


    public void drawLine(Graphics g){
        int x1=random.nextInt(this.width);
        int y1=random.nextInt(this.height);
        int x2=random.nextInt(15);
        int y2=random.nextInt(15);
        g.drawLine(x1,  y1, x1+ x2,y1+y2);//绘制直线
        //  g.translate(random.nextInt(3), random.nextInt(3));

    }


    public Color getColor(int x,int y){
        if(x>255)
            x=255;
        if(y>255)
            y=255;

        int r=x+random.nextInt(y-x);
        int g=x+random.nextInt(y-x);
        int b=x+random.nextInt(y-x);

        return new Color(r,g,b);
    }
}
